/**销户
 * Created by evan on 2017/9/26.
 */
var comValidform=null;
var closeAccountStatus=false;
var closeAccount ={
    userId:null, //要销户的用户ID
    userInfo:null, //用户信息
    id:"",
    codeActive:true, //是否能发验证码

    init:function () {
        closeAccount.dataHandle(); // 数据初始化
        closeAccount.codeHandle(); //验证码操作绑定
        closeAccount.submitHandle(); //表单提交的操作
    },


    /**
     * 数据的初始化
     */
    dataHandle:function () {
        closeAccount.userId=art.dialog.data('closeAccountId');  //修改id
        closeAccount.id = closeAccount.userId+"_User_Key";
        closeAccount.userInfo = eval('(' + localStorage.getItem(closeAccount.id) + ')');
        $(".userName").html(closeAccount.userInfo.realName);
        if(closeAccount.userInfo.closeAccount == 1){
            alert("已销户的用户不能再进行销户！");
            art.dialog.close();
        }
    },
    /**
     * 验证码操作
     */
    codeHandle:function () {
        $(".code").on("click",function () {
            if(closeAccount.codeActive == false)
                return;

            $(".code").addClass("unable").removeClass("able");
            closeAccount.setTime();
            closeAccount.sendCode();
        })
    },
    /**
     * 发送验证码
     */
    sendCode:function () {
        $.ajax({
            type:'GET',
            url: '/mgr/safetyCtrl/getCloseAccountCode?userId='+ closeAccount.userId,
            contentType : 'application/json',
            dataType : 'json',
            timeout : 30000, // 30秒超时
            success : function (data) {
                if (data.resultCode == 200) {
                    layer.msg("发送成功");
                }else{
                    layer.msg(data.msg);
                }
            },
            error:function (result) {
                console.log(result);
                layer.msg("发送失败！");
            }
        })
    },
    /**
     * 验证码倒计时
     */
    setTime:function () {
        var waitTime = 120;
        time();
        function time() {
            if(waitTime == 0){
                $(".code").html("获取验证码") ;
                closeAccount.codeActive = true;
                $(".code").removeClass("unable").addClass("able");
                return;
            }else{
                $(".code").html(waitTime+"秒后重发")
                closeAccount.codeActive = false;
                waitTime --;
            }
            setTimeout(function () {
                time()
            },1000);
        }
    },
    /**
     * 提交表单
     */
    submitHandle:function () {
        comValidform = $(".com-addForm").Validform({
            dataType:{

            },
            tiptype:2,
            tipSweep:true,
            beforeSubmit:function () {
                if(!$("#fuyou").prop("checked")){
                    alert("富友还没有确认销户，不能进行销户！");
                    return;
                }
                if(!$("#yingmi").prop("checked")){
                    alert("盈米还没有确认销户，不能进行销户！");
                    return;
                }
                if(!$("#huochai").prop("checked")){
                    alert("火柴还没有确认销户，不能进行销户！");
                    return;
                }

                var url = 'mgr/SysUserCtrl/closeAccount';
                var params ={
                    "actUserId":userId,
                    "userId":closeAccount.userId,
                    "code01":$(".code1").val(),
                    "code02":$(".code2").val()
                }
                var cb = function (data) {
                    //成功返回
                    if(data.resultCode==200){
                        //关闭窗口并刷新数据
                        //art.dialog.close();
                        localStorage.removeItem(closeAccount.id);
                        var singleObj = data.result;

                        var showInfo = singleObj.realName;
                        if(showInfo == null || showInfo == ""){
                            showInfo = singleObj.name;
                        }
                        alert(showInfo+"已销户成功");

                        localStorage.setItem(singleObj.id
                            + "_User_Key", JSON
                            .stringify(singleObj));
                        closeAccountStatus=true;
                    }else{
                        layer.msg(data.msg);
                    }
                }
                sendApiNoLoadSync(url, params, cb, 'POST');
                return closeAccountStatus;
            }

        })
    }

};
$(document).ready(function() {
    closeAccount.init();
});

/**
 * 提交表单
 */
function submitForm(){
    comValidform.submitForm();
    return closeAccountStatus;
}
